package com.jinzhi.jzweb.controller;


import java.util.*;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.ZykTypeResourcesDO;
import com.jinzhi.jzweb.service.ZykTypeResourcesService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 带类型的资源表该表没有章节直接与资源管理表对应
 * </pre>
 * <small> 2024-01-12 10:43:30 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/zykTypeResources")
public class ZykTypeResourcesController extends BaseController {
	@Autowired
	private ZykTypeResourcesService zykTypeResourcesService;
	
	@GetMapping()
	String ZykTypeResources(){
	    return "jzweb/zykTypeResources/zykTypeResources";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<ZykTypeResourcesDO>> list(ZykTypeResourcesDO zykTypeResourcesDTO){
		EntityWrapper<ZykTypeResourcesDO> wrapper =new EntityWrapper<>();
		if (zykTypeResourcesDTO.getName()!=null&&!zykTypeResourcesDTO.getName().equals("")) {
			wrapper.like("zy_name", "%" + zykTypeResourcesDTO.getName() + "%");
		}
		if (zykTypeResourcesDTO.getManagementResourcesId()!=null&&!zykTypeResourcesDTO.getManagementResourcesId().equals("")) {
			wrapper.eq("management_resources_id", zykTypeResourcesDTO.getManagementResourcesId());
		}
		wrapper.orderBy("zy_sort", false);
		wrapper.orderBy("creater_date", false);
		// 查询列表数据
		Page<ZykTypeResourcesDO> page = zykTypeResourcesService.selectPage(getPage(ZykTypeResourcesDO.class),wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				ZykTypeResourcesDO obj = (ZykTypeResourcesDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	String add(){
	    return "jzweb/zykTypeResources/add";
	}


	@GetMapping("/add2")
	String add2(Long id,Model model){
		ZykTypeResourcesDO zykTypeResources = zykTypeResourcesService.selectById(id);
		model.addAttribute("zykTypeResources", zykTypeResources);
		return "jzweb/zykTypeResources/edit";
	}

	@Log("添加带类型的资源表该表没有章节直接与资源管理表对应")
	@ResponseBody
	@PostMapping("/save")
	public Result<String> save( ZykTypeResourcesDO zykTypeResources){
		zykTypeResources.setCreaterDate(new Date());
		zykTypeResources.setCreaterName(this.getUsername());
		zykTypeResourcesService.insert(zykTypeResources);
        return Result.ok();
	}
	
	@Log("修改带类型的资源表该表没有章节直接与资源管理表对应")
	@ResponseBody
	@RequestMapping("/update")
	public Result<String>  update( ZykTypeResourcesDO zykTypeResources){
		zykTypeResources.setUpdateDate(new Date());
		zykTypeResources.setUpdateName(this.getUsername());
		boolean update = zykTypeResourcesService.updateById(zykTypeResources);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除带类型的资源表该表没有章节直接与资源管理表对应")
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Long id){
		zykTypeResourcesService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除带类型的资源表该表没有章节直接与资源管理表对应")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		zykTypeResourcesService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
